package com.aier.common.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CityBean;
import com.aier.common.common.CityUtil;
import com.aier.common.service.ISysCityService;

@Scope("prototype")
@Component("commonCityUtilAction")
public class CommonCityUtilAction extends CommonBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int cityid;
    private String cityname;

    private String dvid;

    private Map<Integer, String> pl;
    private Integer pv;
    private Map<Integer, String> cl;
    private Integer cv;
    private Map<Integer, String> al;
    private Integer av;
    private Map<Integer, String> tl;
    private Integer tv;
    private int count;
    private int s;
    private short ct;
    private List<String> subname;

    private boolean jaddress;

    public String list() {
        return "list";
    }

    public String gsub() {
        CityBean cb = CityUtil.getCityBean(cityid);
        if (null == cb) {
            pl = CityUtil.getProvince();
            pv = -1;
            s = 1;
            return SUCCESS;
        }
        Integer subid = cb.getSubid();
        switch (cb.getCtype()) {
            case ISysCityService.CTYPE_TOWN:
                tl = CityUtil.getSubCityMap(subid);
                tv = cb.getId();
                cb = CityUtil.getCityBean(cb.getSubid());
                subid = cb.getSubid();
            case ISysCityService.CTYPE_AREA:
                al = CityUtil.getSubCityMap(subid);
                av = cb.getId();
                cb = CityUtil.getCityBean(cb.getSubid());
                subid = cb.getSubid();
            case ISysCityService.CTYPE_CITY:
                cl = CityUtil.getSubCityMap(subid);
                cv = cb.getId();
                subid = CityUtil.getCityBean(cv).getSubid();
            case ISysCityService.CTYPE_PROVINCE:
                pl = CityUtil.getProvince();
                if (subid > 0) {
                    pv = subid;
                } else if (cityid > 0) {
                    pv = cityid;
                }
                break;
            default:
                s = -1;
                return SUCCESS;
        }
        s = 1;
        return SUCCESS;
    }

    @Override
    public String execute() {
        if (cityid > 0) {
            loadcity();
        } else {
            cityid = -1;
            ct = 0;
            return "list";
        }
        return SUCCESS;
    }

    public String load() {
        if (cityid <= 0) {
            cl = CityUtil.getProvince();
            s = 1;
            ct = 0;
            loadSubCityName();
            return SUCCESS;
        }
        loadcity();
        loadSubCityName();
        s = 1;
        return SUCCESS;
    }

    private void loadcity() {
        CityBean cb = CityUtil.getCityBean(cityid);
        if (null == cb) {
            s = 0;
            ct = 0;
            return;
        }

        cityname = cb.getName();
        ct = cb.getCtype();
        cl = CityUtil.getCity(cityid);
        if (null == cl || cl.size() <= 0) {
            count = 0;
        } else {
            count = cl.size();
        }
    }

    private void loadSubCityName() {
        CityBean cb = CityUtil.getCityBean(cityid);
        subname = new ArrayList<String>();
        subname.add(cb.getName());
        while (cb.getSubid() > 0) {
            cb = CityUtil.getCityBean(cb.getSubid());
            if (null == cb) {
                break;
            }
            subname.add(cb.getName());
        }
    }

    public int getCityid() {
        return cityid;
    }

    public void setCityid(int cityid) {
        this.cityid = cityid;
    }

    public String getDvid() {
        return dvid;
    }

    public void setDvid(String dvid) {
        this.dvid = dvid;
    }

    public Map<Integer, String> getCl() {
        return cl;
    }

    public int getS() {
        return s;
    }

    public short getCt() {
        return ct;
    }

    public int getCount() {
        return count;
    }

    public boolean isJaddress() {
        return jaddress;
    }

    public void setJaddress(boolean jaddress) {
        this.jaddress = jaddress;
    }

    public String getCityname() {
        return cityname;
    }

    public List<String> getSubname() {
        return subname;
    }

    public Map<Integer, String> getPl() {
        return pl;
    }

    public Map<Integer, String> getAl() {
        return al;
    }

    public Map<Integer, String> getTl() {
        return tl;
    }

    public Integer getPv() {
        return pv;
    }

    public Integer getCv() {
        return cv;
    }

    public Integer getAv() {
        return av;
    }

    public Integer getTv() {
        return tv;
    }

}
